import { Injectable } from "@nestjs/common";
import { Strategy,ExtractJwt } from 'passport-jwt' 
import { PassportStrategy } from "@nestjs/passport";
import { ConfigService } from "@nestjs/config";
import { PrismaService } from "src/prisma/prisma.service";

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy,'jwt'){
    constructor(configService:ConfigService,private prisma:PrismaService){
        super({
            // 解析用户提交的 Bearer Token header 数据
            jwtFromRequest:ExtractJwt.fromAuthHeaderAsBearerToken(),
            // 加密码的 secret
            secretOrKey:configService.get('TOKEN_SECRET')
        })
    }

    // 验证通过后结果用户资料
    async validate({sub:id}){
        return this.prisma.user.findUnique({
            where:{ id }
        })
    }
}